package com.tgy.JUC.FinalLock;

import java.util.concurrent.TimeUnit;

/***
 * @ClassName: DeadLock
 * @Description: 测试死锁
 * @Auther: 送你一块二向箔
 * @Date: 16:43 2022/3/16
 * @version : V1.0
 */
public class DeadLock {
    public static void main(String[] args) {
        MakeUp makeUp1 = new MakeUp();
        new Thread(() -> {
            makeUp1.run1();
        },"====1").start();
        new Thread(() -> {
            makeUp1.run2();
        },"$$$$$$2").start();
    }


}

class MakeUp {
    private final StringBuffer stringBuffer = new StringBuffer("=====");
    private final StringBuffer stringBuffer1 = new StringBuffer("23333");

    public void run1() {
        synchronized (stringBuffer) {
            stringBuffer.append("你好");
            System.out.println("run1 获取了字符锁"+stringBuffer);
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("睡眠结束");
            synchronized (stringBuffer1) {
                stringBuffer1.append("000000000");
                System.out.println("run1 获取了数字锁" + stringBuffer1);
            }
        }
    }

    public void run2() {
        synchronized (stringBuffer1) {
            stringBuffer1.append("23333");
            System.out.println("run2 获取了数字锁"+stringBuffer1);
            synchronized (stringBuffer) {
                stringBuffer.append("hello");
                System.out.println("run2 获取了字符锁"+stringBuffer);
            }
        }
    }
}
